// pages/to-pay-order/index.js
const WXAPI = require('apifm-wxapi')
const CONFIG = require('../../config.js')
Page({

  /**
   * 页面的初始数据
   */
  data: {
    price:0,
    orderlist:[],
    addressData:{},
    postStatus:"kuai",
    shoparr:[],
    index:0,
    goodsJsonStr:[],//订单数组
    peisongStatus:'kd',
    remark:'',//备注

  },
  addAddress(){
    wx.navigateTo({
      url: '../../pages/address-add/index'
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    WXAPI.init(CONFIG.subDomain)
 
  },
  // 加载地址方法
  loadAddress(){
     wx.request({
       url: 'https://api.it120.cc/ljf/user/shipping-address/default/v2',
       method:"get",
       data:{
         token:wx.getStorageSync('token')
       },
       success:res=>{
         console.log(res)
         this.setData({
          addressData:res.data.data
         })
       }
     })
  },
  turnselect(){
    wx.navigateTo({
      url: '../../pages/select-address/index',
    })
  },
  // 获取订单页列表数据
  async loadorderList(){
      wx.request({
        url: 'https://api.it120.cc/ljf/shopping-cart/info',
        method:"get",
        data:{
          token:wx.getStorageSync('token')
        },
        success:(res)=>{
            console.log(res)
            this.setData({
              price:res.data.data.price,
              orderlist:res.data.data.items
            })
            var arr=[]
            this.data.orderlist.forEach(item=>{
              arr.push(item)
            })
            this.setData({
              goodsJsonStr:arr
            })
           
        } 
      })
    
  },
  remarkchange(e){//更改备注
    this.setData({
      remark:e.detail.value
    })
  },
  createOrder(){//创建订单
    var postdata={
           token:wx.getStorageSync('token'),
           goodsJsonStr:JSON.stringify(this.data.goodsJsonStr),
          remark: this.data.remark,
          peisongType: this.data.postStatus=="kuai"?'kd':'zq',
          provinceId: this.data.addressData.provinceId,
          cityId: this.data.addressData.cityId,
          districtId: this.data.addressData.districtId,
          address: this.data.addressData.address,
          linkMan: this.data.addressData.linkMan,
          mobile: this.data.addressData.mobile,
          code: 322000 
    };
      console.log(postdata)
      wx.request({
        url: 'https://api.it120.cc/ljf/order/create',
        method:"post",
        header: {
          'Content-Type': 'application/x-www-form-urlencoded'
        },
        data:postdata,
        success:res=>{
          console.log(res)
          wx.showToast({
            title: '提交订单成功',
          })
        }
      })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },
  radiochange(e){
   
      this.setData({
        postStatus:e.detail.value 
      })
      console.log(this.data.postStatus)
  },
  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    this.loadorderList()
    this.loadAddress();
    this.loadShop()
  },
  shopchange(e){
      console.log(e.detail.value)
      this.setData({
        index:e.detail.value
      })
  },
  // 加载门店数据
  loadShop(){
      wx.request({
        url: 'https://api.it120.cc/ljf/shop/subshop/list',
        method:"post",
        header:{
          'Content-type':'application/x-www-form-urlencoded'
        },
        success:res=>{
          console.log(res)
          if(res.data.code==0){
            var arr=res.data.data;
            arr.unshift({index:'0',name:"请选择"})
            this.setData({
              shoparr:arr
            })
          }
        }
      })
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})